<template>
  <el-card>
    <!-- 容器el-card > 面包屑 + 添加按钮 + 核心表格 -->
    <!-- 面包屑 TODO 将来要二次封装 这里仅展示即可 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>活动管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 添加按钮 -->
    <!-- el-row > el-col 默认24栏 支持flex布局 -->
    <el-row type="flex" justify="end" style="margin-bottom: 10px">
      <el-col :span="2">
        <el-button
          size="mini"
          type="primary"
          plain
          @click="dialogFormVisibleAdd = true"
          >添加分类</el-button
        >
      </el-col>
    </el-row>
    <!--
    表格
      :data="要绑定的数组数据"
      border 表格边框
      stripe 斑马纹
    -->
    <el-table :data="artCateList" border stripe size="mini" style="width: 100%">
      <!-- type="index" 自动从1开始的序号 -->
      <el-table-column type="index" label="序号" width="180"> </el-table-column>
      <el-table-column prop="cate_name" label="分类名称" width="180">
      </el-table-column>
      <el-table-column prop="cate_alias" label="分类名称"> </el-table-column>
      <el-table-column label="操作">
        <!-- 插槽 => 作用域插槽 -->
        <!-- <template #default="scope"> 等价于下面的写法-->
        <!-- scope.row 是 el-table的data属性绑定的数组中对应的对象{} -->
        <template slot-scope="scope">
          <!-- {{ scope.row }} -->
          <el-button size="mini" @click="showDiaEdit(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="showConfirm(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!--
    对话框--添加分类
          @close
          @open
          width="百分数"
     -->
    <el-dialog
      title="添加分类"
      :visible.sync="dialogFormVisibleAdd"
      width="35%"
      @close="resetForm"
    >
      <el-form
        :model="formAdd"
        :rules="addRules"
        ref="addRef"
        label-width="80px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="formAdd.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="formAdd.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleAdd = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </div>
    </el-dialog>

    <!--
      编辑的对话框
     -->
    <el-dialog
      title="编辑分类"
      :visible.sync="dialogFormVisibleEdit"
      width="35%"
    >
      <el-form
        :model="formEdit"
        :rules="addRules"
        ref="editRef"
        label-width="80px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="formEdit.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="formEdit.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleEdit = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import { mapActions, mapState } from 'vuex'
import {
  delArtCateById,
  articleCategoryPost,
  articleCategoryGetById,
  editArticleCategoryByEditForm
} from '@/api/article.js'

export default {
  name: 'ArtCategory',
  data () {
    return {
      // 控制对话框显隐
      dialogFormVisibleAdd: false,
      dialogFormVisibleEdit: false,

      // 添加分类的表单
      formAdd: {
        cate_name: '',
        cate_alias: ''
      },
      // 编辑分类的表单
      formEdit: {
        id: 1,
        cate_name: '',
        cate_alias: ''
      },
      // 添加/编辑共同的表单验证规则
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名称必须是1-10位的非空字符串',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    // 获取分类列表数据
    this.getArtCateList()
  },
  computed: {
    ...mapState('article', ['artCateList'])
  },
  methods: {
    ...mapActions('article', ['getArtCateList']),

    // 对话框中的确定按钮 => 编辑分类
    async editCate () {
      // 1. 请求 => api => put
      const {
        data: { code, message }
      } = await editArticleCategoryByEditForm(this.formEdit)
      if (code !== 0) return this.$message.error(message)
      this.$message.success(message)
      // 2. 关闭
      this.dialogFormVisibleEdit = false
      // 3. 更新视图
      this.getArtCateList()
    },

    // 操作中的编辑按钮 => 展示编辑的对话框
    async showDiaEdit (id) {
      // 打开对话框
      this.dialogFormVisibleEdit = true
      // 请求
      const {
        data: { code, message, data = {} }
      } = await articleCategoryGetById(id)
      if (code !== 0) return this.$message.error(message)
      // 回显数据
      this.formEdit = data
    },
    // 操作中的删除按钮 => 展示确认框
    showConfirm (id) {
      if (id === 1 || id === 2) return this.$message.error('小可爱 不要动')
      // 打开确认框
      this.$confirm('Sure?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 确认 删除当前分类
          const {
            data: { code, message }
          } = await delArtCateById(id)
          if (code !== 0) return this.$message.error(message)
          this.$message.success(message)
          //  更新视图
          this.getArtCateList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    },
    resetForm () {
      // 重置表单
      // 思路1
      // this.formAdd.xxx = ''
      // 思路2 查文档
      this.$refs.addRef.resetFields()
    },
    // 添加分类
    addCate () {
      // 校验 => 请求 或者  提示
      this.$refs.addRef.validate(async valid => {
        if (!valid) return
        const {
          data: { code, message }
        } = await articleCategoryPost(this.formAdd)
        if (code === 1) return console.log(message)
        // 添加成功
        this.getArtCateList()
        // 关闭对话框
        this.dialogFormVisibleAdd = false
      })
    }
  }
}
</script>

<style scoped></style>
